import { WeilaNetworkInterface} from 'weilasdk_lite';

export class WeilaMiniNetwork implements WeilaNetworkInterface {
    private socket: WechatMiniprogram.SocketTask|null;
    private onConnectCb: (() => void)|null;
    private onDisconnectCb: (() => void)|null;
    private onRecvCb: ((data: Uint8Array) => void)|null;
    private onErrorCb: ((error: any) => void)|null;

    constructor() {
        this.socket = null;
        this.onConnectCb = null;
        this.onDisconnectCb = null;
        this.onRecvCb = null;
        this.onErrorCb = null;
    }

    public close(): void {
        if (this.socket == null) {
            console.log("socket is null");
            return;
        }
        
        this.socket.close({
        });
    }

    public send(data: Uint8Array): void {
        wx.sendSocketMessage({
            data: data.buffer,
            success: (res: any) => {
                console.log("success is ", res);
            },
            fail: (error: any) => {
                console.log("fail is ", error);
            }
        })
    }

    public connect(url: string): void {

        console.log('connect ', url);
        
        wx.connectSocket({
            url: url,
            tcpNoDelay: true,
            timeout: 10000,
            success: (res: any) => {
                console.log('connect succ', res);

                wx.onSocketClose(this.onSocketClose.bind(this))
                wx.onSocketMessage(this.onSocketRecv.bind(this))
                wx.onSocketError(this.onSocketError.bind(this))
                wx.onSocketOpen(this.onSocketOpen.bind(this));
            },

            fail: (error: any) => {
                console.log("connect error is", error);
                if (this.onErrorCb) {
                    this.onErrorCb(JSON.stringify(error));
                }
            }
        })
    }


    public disconnect(): void {
        wx.closeSocket({
            code: 0,
            reason: 'normal'
        });
    }


    public onConnect(onConnect: () => void): void {
        this.onConnectCb = onConnect;
    }

    public onClose(onClose: () => void): void {
        this.onDisconnectCb = onClose;
    }

    public onError(onError: (error: any) => void): void {
        this.onErrorCb = onError;
    }

    public onRecv(onRecv: (data: Uint8Array) => void): void {
        this.onRecvCb = onRecv;
    }

    private onSocketOpen(event: any) {
        console.log('服务器连接成功', event);
        if (this.onConnectCb) {
            this.onConnectCb();
        }
    }

    private onSocketRecv(event: any) {
        console.log('收到消息', event);
        if (this.onRecvCb) {
            this.onRecvCb(new Uint8Array(event.data));
        }
    }

    private onSocketClose(event: any) {
        console.log('服务器连接断开', event);
        if (this.onDisconnectCb) {
            this.onDisconnectCb();
        }
    }   

    private onSocketError(err: any) {
        console.log('服务器连接出错');
        if (this.onErrorCb) {
            this.onErrorCb(JSON.stringify(err.errMsg));
        }
    }
}